package com.ruoyi.project.game.recommend.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.game.game.domain.Game;
import com.ruoyi.project.game.game.mapper.GameMapper;
import com.ruoyi.project.game.recommend.domain.Recommend;
import com.ruoyi.project.game.recommend.mapper.RecommendMapper;
import com.ruoyi.project.game.recommend.service.IRecommendService;
import com.ruoyi.project.game.recommendhistory.domain.RecommendHistory;
import com.ruoyi.project.game.recommendhistory.mapper.RecommendHistoryMapper;

/**
 * 游戏推荐Service业务层处理
 * 
 * @author shixiangwen
 * @date 2020-02-24
 */
@Service
@DataSource(value = DataSourceType.GAME)
public class RecommendServiceImpl implements IRecommendService {

	@Autowired
	private RecommendMapper recommendMapper;

	@Autowired
	private RecommendHistoryMapper recommendHistoryMapper;

	@Autowired
	GameMapper gameMapper;

	/**
	 * 查询游戏推荐
	 * 
	 * @param id 游戏推荐ID
	 * @return 游戏推荐
	 */
	@Override
	public Recommend selectRecommendById(Long id) {
		return recommendMapper.selectRecommendById(id);
	}

	/**
	 * 查询游戏推荐列表
	 * 
	 * @param recommend 游戏推荐
	 * @return 游戏推荐
	 */
	@Override
	public List<Recommend> selectRecommendList(Recommend recommend) {
		List<Recommend> list = recommendMapper.selectRecommendList(recommend);
		for (Recommend item : list) {
			Game game = gameMapper.selectGameById(item.getGameId());
			if (game != null) {
				item.setName(game.getName());
				item.setIcon(game.getIcon());
			}
		}
		return list;
	}

	/**
	 * 新增游戏推荐
	 * 
	 * @param recommend 游戏推荐
	 * @return 结果
	 */
	@Override
	public int insertRecommend(Recommend recommend) {
		recommend.setCreateTime(DateUtils.getNowDate());
		return recommendMapper.insertRecommend(recommend);
	}

	/**
	 * 修改游戏推荐
	 * 
	 * @param recommend 游戏推荐
	 * @return 结果
	 */
	@Override
	public int updateRecommend(Recommend recommend) {
		recommend.setUpdateTime(DateUtils.getNowDate());
		return recommendMapper.updateRecommend(recommend);
	}

	/**
	 * 删除游戏推荐对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteRecommendByIds(String ids) {
		return recommendMapper.deleteRecommendByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除游戏推荐信息
	 * 
	 * @param id 游戏推荐ID
	 * @return 结果
	 */
	@Override
	public int deleteRecommendById(Long id) {
		return recommendMapper.deleteRecommendById(id);
	}

	@Override
	public boolean addRecommend(Long gameId) {
		Recommend recommend = recommendMapper.selectRecommendByGameId(gameId);
		if (recommend != null) {
			return true;
		}
		recommend = new Recommend();
		recommend.setCreateBy(ShiroUtils.getLoginName());
		recommend.setCreateTime(DateUtils.getNowDate());
		recommend.setGameId(gameId);
		recommend.setSort(0L);
		return recommendMapper.insertRecommend(recommend) > 0;
	}

	@Override
	public boolean delRecommend(Long gameId) {

		Recommend recommend = recommendMapper.selectRecommendByGameId(gameId);
		if (recommend == null) {
			return false;
		}

		RecommendHistory recommendHistory = recommendHistoryMapper.selectRecommendHistoryByGameId(gameId);
		if (recommendHistory == null) {
			recommendHistory = new RecommendHistory();
			recommendHistory.setCreateBy(ShiroUtils.getLoginName());
			recommendHistory.setCreateTime(DateUtils.getNowDate());
			recommendHistory.setGameId(gameId);
			recommendHistory.setHotSearchTime(DateUtils.getNowDate());
			recommendHistory.setHotSearch(0L);
			recommendHistoryMapper.insertRecommendHistory(recommendHistory);
		}
		return recommendMapper.deleteRecommendById(recommend.getId()) > 0;
	}

}
